package com.snowher.play.controller;

import com.snowher.play.entity.UserAccessEntity;
import com.snowher.play.service.IUserService;
import com.snowher.play.utils.JwtUtils;
import com.snowher.play.utils.TextUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Author: by HeJingzhou on 2018/9/25 10:49.
 * <p>
 * Company: 北京天创时代信息技术有限公司
 * <p>
 * Email: tcowork@163.com
 * <p>
 * 应用:
 *
 * @author 贺景洲
 */
@Controller
@RequestMapping(value = "/User")
public class UserController {

    @Resource
    IUserService mUserService;

    /**
     * 注册用户
     *
     * @param httpServletRequest Servlet
     * @param userName           用户名
     * @param password           密码
     *                           http://localhost:8080/User/registerUser?userName=hejingzhou&password=1230
     */
    @RequestMapping(value = "/registerUser")
    public void registerUser(HttpServletRequest httpServletRequest, @Param("userName") String userName, @Param("password") String password) {
        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(password)) {
            return;
        }
        UserAccessEntity userRegister = new UserAccessEntity();
        userRegister.setUserName(userName);
        userRegister.setPassword(password);
        int itemNum = mUserService.registerUser(userRegister);
        System.out.println(itemNum == 1 ? "注册成功" : "注册失败");
    }

    /**
     * 账户登录
     *
     * @param httpServletRequest Servlet
     * @param userName           用户名
     * @param password           密码
     *                           http://localhost:8080/User/login?userName=hejingzhou&password=1230
     */
    @RequestMapping(value = "/login")
    public void loginUser(HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest, @Param("userName") String userName, @Param("password") String password) {
        List<UserAccessEntity> userAccessList = mUserService.loginUser(new UserAccessEntity(userName, password));
        if (userAccessList == null || userAccessList.size() == 0) {
            System.out.println("暂无此用户");
        }
        if (userAccessList.size() == 1) {
            UserAccessEntity userAccessEntity = userAccessList.get(0);
            System.out.println(userAccessEntity.toString());
            Cookie[] cookies = httpServletRequest.getCookies();
            httpServletResponse.addCookie(new Cookie("token", JwtUtils.createToken(String.valueOf(userAccessEntity.getId()))));
        }
    }

    /**
     * 测试需要Token
     *
     * @param httpServletRequest servlet
     */
    @RequestMapping(value = "/needToken")
    public void needUserToken(HttpServletRequest httpServletRequest) {
        Cookie[] cookies = httpServletRequest.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (cookie != null) {
                if ("token".equals(cookie.getName())) {
                    String tokenValue = cookie.getValue();
                    String userId = JwtUtils.parser(tokenValue);
                    System.out.println("浏览器携带的Cookie中的值Token：" + userId);
                }
            }
        }
    }

}
